<template>
  <div class="echarts">
    <div class="echartsTop">
      <div id="myChart"></div>
    </div>
    <el-row class="echartsFoot" type="flex" justify="space-around">
      <el-col :span="12" class="ranking">
        <div class="rankingLeft">
          <h2>排行榜</h2>
          <el-table :data="tableData" style="width: 100%;overflow:auto" height="420">
            <el-table-column prop="name" label="姓名"></el-table-column>
            <el-table-column prop="count" label="学员人数"></el-table-column>
          </el-table>
        </div>
      </el-col>
      <el-col :span="12" class="pie">
        <div id="main"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imgurl:"",
      tableData: [],
      imgsrcurl:"",
      arrbt:"",
      arrindex:"",
      dataqk:[],
    };
  },
  mounted() {
    this.drawLineBar();
    this.drawPie();
    this.topSort();
  },
  methods: {
    //排行榜
    async topSort(){
      let phb = await this.$api.home.topSort()
      this.tableData = phb.data.data
    },
    //获取未结伴的班级情况
    async drawLineBar() {
      // 基于准备好的dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById("myChart"));
      let endclass = (await this.$api.home.endClass()).data
      let a = JSON.parse(JSON.stringify(endclass.arr))
      let numbera = JSON.parse(JSON.stringify(endclass.arr1))
      // 绘制图表
      var option = {
        title: {
          text: "班级情况"
        },
        tooltip: {
          trigger: "axis"
        },
        toolbox: {
          show: true,
          feature: {
            dataZoom: {
              yAxisIndex: "inside"
            }, //区域缩放，区域缩放还原
            dataView: {
              readOnly: false
            }, //数据视图
            magicType: {
              type: ["line", "bar"]
            }, //切换为折线图，切换为柱状图
            restore: {}, //还原
            saveAsImage: {}, //保存为图片
            
          }
        },

        xAxis: {
          type: "category",
          data: a
        },
        yAxis: {
          type: "value"
        },
        series: [
          {
            data: numbera,
            type: "bar",
            barWidth: 60
          }
        ]
      };
      myChart.setOption(option);
      setTimeout(()=>{
        this.imgurl = myChart.getDataURL()
      },1000)
      var imgsrc = new Image()
      this.imgsrcurl = imgsrc
      this.imgsrcurl=myChart.getDataURL({
        pixelRatio: 2,
        backgroundColor: '#fff'
      })
      // console.log("this.imgsrcurl",this.imgsrcurl);

    },
    async drawPie() {
      // 基于准备好的dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById("main"));
      let mantj = (await this.$api.home.manLine()).data
      let a = mantj.data
      var option = {
        title: {
          text: "人员情况",
          left: "center"
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left"
        },
        series: [
          {
            name: "人数",
            type: "pie",
            radius: "50%",
            data: a,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      };
      myChart.setOption(option);
    }
  }
};
</script>

<style lang="less" scope>
#main {
  width: 500px;
  height: 440px;
  margin: 10px 135px;
}
#myChart {
  width: 98%;
  height: 500px;
}
.echarts {
  height: 100%;
  .echartsTop {
    border: 1px solid #ccc;
    padding: 20px;
    border-radius: 5px;
    margin-right: 16px;
  }
  .echartsFoot {
    margin-top: 20px;
    margin-right: 16px;
    .pie {
      border: 1px solid #ccc;
      padding: 20px;
      border-radius: 5px;
      display: flex;
      justify-content: center;
      margin-bottom: 20px;
    }
  }
}
.ranking {
  padding: 0 20px 20px 0;
  .rankingLeft {
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    h2 {
      margin-bottom: 10px;
    }
  }
}
</style>